﻿#include "SpittingToad.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Enums.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    SpittingToad::SpittingToad(MonsterInfo *info) : MonsterObject(info)
    {
        ActionTime = getEnvir()->getTime() + 1000;
    }

    std::string SpittingToad::getName() const
    {
        return Master == nullptr ? Info->getGameName() : (Dead ? Info->getGameName() : StringHelper::formatSimple("{0}({1})", Info->getGameName(), Master->getName()));
    }

    void SpittingToad::setName(const std::string &value)
    {
        throw NotSupportedException();
    }

    void SpittingToad::Process()
    {
        if (!Dead && Summoned)
        {
            if (Master != nullptr)
            {
                bool selfDestruct = false;
                if (FindObject(Master->ObjectID, 15) == nullptr)
                {
                    selfDestruct = true;
                }
                if (Summoned && getEnvir()->getTime() > AliveTime)
                {
                    selfDestruct = true;
                }
                if (selfDestruct && Master != nullptr)
                {
                    Die();
                }
            }
        }
        MonsterObject::Process();
    }

    void SpittingToad::Process(DelayedAction *action)
    {
        switch (action->Type)
        {
            case DelayedType::Damage:
                CompleteAttack(action->Params);
                break;
            case DelayedType::RangeDamage:
                CompleteRangeAttack(action->Params);
                break;
            case DelayedType::Recall:
                PetRecall(std::any_cast<MapObject*>(action->Params[0]));
                break;
        }
    }

    void SpittingToad::PetRecall(MapObject *target)
    {
        if (target == nullptr)
        {
            return;
        }
        if (Master == nullptr)
        {
            return;
        }
        Teleport(Master->getCurrentMap(), target->getCurrentLocation());
    }

    void SpittingToad::ProcessAI()
    {
        //ProcessSearch
        if (getEnvir()->getTime() < SearchTime)
        {
            return;
        }
        SearchTime = getEnvir()->getTime() + SearchDelay;

        if (getTarget() == nullptr)
        {
            FindTarget();
        }

        //ProcessTarget           
        if (getTarget() == nullptr || !getCanAttack())
        {
            return;
        }

        if (InAttackRange())
        {
            Attack();
            if (getTarget()->Dead)
            {
                FindTarget();
            }
            return;
        }
        else
        {
            FindTarget(); //Target out of range find new
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }
    }

    bool SpittingToad::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        int x = std::abs(getTarget()->getCurrentLocation()->X - getCurrentLocation()->X);
        int y = std::abs(getTarget()->getCurrentLocation()->Y - getCurrentLocation()->Y);

        if (x > 12 || y > 12)
        {
            return false;
        }


        return (x <= 12 && y <= 12) || (x == y || x % 2 == y % 2);
    }

    void SpittingToad::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;
        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        Turn(getDirection());
        S::ObjectRangeAttack *tempVar = new S::ObjectRangeAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        tempVar->TargetID = getTarget()->ObjectID;
        Broadcast(tempVar);
        AttackTime = getEnvir()->getTime() + AttackSpeed + 500;
        if (damage == 0)
        {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            return;
        }

        int delay = Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation()) * 50 + 500; //50 MS per Step

        DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + delay, {getTarget(), damage, DefenceType::MAC});
        ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void SpittingToad::Spawned()
    {
        MonsterObject::Spawned();
        Summoned = true;
    }

    Packet *SpittingToad::GetInfo()
    {
        S::ObjectMonster *tempVar = new S::ObjectMonster();
        tempVar->ObjectID = ObjectID;
        tempVar->Name = getName();
        tempVar->NameColour = NameColour;
        tempVar->Location = getCurrentLocation();
        tempVar->Image = Monster::SpittingToad;
        tempVar->Direction = getDirection();
        tempVar->Effect = Info->Effect;
        tempVar->AI = Info->AI;
        tempVar->Light = Info->Light;
        tempVar->Dead = Dead;
        tempVar->Skeleton = Harvested;
        tempVar->Poison = CurrentPoison;
        tempVar->Hidden = getHidden();
        tempVar->Extra = Summoned;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was used in a 'return' or 'throw' statement.
        return tempVar;
    }
}
